import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/pages/home/message/message_controller.dart';
import 'package:flutter_changdian_football/utils/pull/pull_smart_refresher.dart';
import 'package:flutter_changdian_football/widget/dialog/message_dialog.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:get/get.dart';

/// 消息盒子
class MessagePage extends StatelessWidget {
  const MessagePage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MessageController>(
      init: MessageController(),
      builder: (messageController) {
        return Scaffold(
          backgroundColor: Colors.white,
          appBar: AppBar(
            centerTitle: true,
            elevation: 0,
            title: Text(
              '消息中心',
              style: TextStyle(fontSize: 16.sp, color: const Color(0xff333333)),
            ),
            iconTheme: const IconThemeData(
              color: Color(0xff333333)
            ),
            backgroundColor: const Color(0xffF8FAF9 ),
            actions: [
              Container(
                alignment: Alignment.center,
                child: Row(
                  children: [
                    GestureDetector(
                      behavior: HitTestBehavior.translucent,
                      onTap: () {
                        messageController.readAll();
                      },
                      child: Stack(
                        children: [
                          Text('全部已读', style: TextStyle(fontSize: 14.sp, color: const Color(0xff499360)),),
                          // Positioned(
                          //   right: 0.w,
                          //   child: Container(
                          //     width: 6.w,
                          //     height: 6.w,
                          //     decoration: BoxDecoration(
                          //       color: const Color(0xffF4333C),
                          //       borderRadius: BorderRadius.circular(6.r)
                          //     ),
                          //   )
                          // )
                        ],
                      ),
                    ),
                    SizedBox(width: 17.w,)
                  ],
                ),
              )
            ],
          ),
          body: RefreshWidget<MessageController>(
            child: ListView.separated(
              itemCount: messageController.messageList.length,
              physics: const BouncingScrollPhysics(),
              separatorBuilder: (context, index) {
                return Container(
                  margin: EdgeInsets.only(left: 71.w),
                  width: double.infinity,
                  height: 1.w,
                  color: const Color(0xffF8FAF9),
                );
              },
              itemBuilder: (BuildContext context, int index) {
                return GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap: () {
                    MessageDialog.show(
                      Get.context!,
                      index: index,
                      confirmTap: () {
                        if ((messageController.messageList[index].messageNum ?? 0) > 0) {
                          Get.back();
                          messageController.readMessage(index);
                        } else {
                          Get.back();
                        }
                      },
                    );
                  },
                  child: Container(
                    padding: EdgeInsets.symmetric(vertical: 15.w, horizontal: 17.w),
                    child: Row(
                      children: [
                        Stack(
                          children: [
                            Container(
                              width: 46.w,
                              height: 46.w,
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(46.r)
                              ),
                              child: ClipRRect(
                                borderRadius: BorderRadius.circular(46.r),
                                child: Image.network(messageController.messageList[index].sendUserImg ?? '', gaplessPlayback: true, excludeFromSemantics: true, fit: BoxFit.fill,),
                              )
                            ),

                            if ((messageController.messageList[index].messageNum ?? 0) > 0) ...[
                              Positioned(
                                right: 0.w,
                                child: Container(
                                  width: 16.w,
                                  height: 16.w,
                                  decoration: BoxDecoration(
                                    color: const Color(0xffF4333C),
                                    borderRadius: BorderRadius.circular(16.r)
                                  ),
                                  alignment: Alignment.center,
                                  child: Text('${messageController.messageList[index].messageNum ?? 0}', style: TextStyle(fontSize: 11.sp, color: Colors.white),),
                                )
                              )
                            ]
                            
                          ],
                        ),
                
                        SizedBox(width: 8.w,),
                
                        Expanded(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              SizedBox(
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    Text(messageController.messageList[index].sendUserNickname ?? '此用户暂无昵称', style: TextStyle(fontSize: 15.sp, color: const Color(0xff333333),), maxLines: 1, overflow: TextOverflow.ellipsis,),
                                    Text(messageController.messageList[index].messageTime ?? '', style: TextStyle(fontSize: 11.sp, color: const Color(0xffcccccc),), maxLines: 1, overflow: TextOverflow.ellipsis,)
                                  ],
                                ),
                              ),
                
                              SizedBox(height: 4.w,),
                
                              SizedBox(
                                child: Text(messageController.messageList[index].content ?? '', style: TextStyle(fontSize: 12.sp, color: const Color(0xffCCCCCC),), maxLines: 1, overflow: TextOverflow.ellipsis,),
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                );
              },
            ),
          ),
        );
      }
    );
  }
}